package com.tianqu.tms.www.member.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.www.member.actionform.MemberInputActionForm;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;
/**
 * 
 * 会员添加分发action
 * 
 * @author tianshifu
 *
 */
public class MemberCreationDispatchAction extends TmsAction {
	private static final String LOADIMG = "imgLoad";//跳转到图片上传
	private static final String TOSAVE = "toSave";//保存
	private static final String POSITION = "position";//图片位置
	private static final String TOCLEAR = "toClear";//清空填入内容 放入一个标识字段证明进入到确认页面了
	
	private Logger logger = Logger.getLogger(MemberCreationDispatchAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		String actionForward = TmsConstants.BLANK;
		//根据页面按钮选择跳转页面
		MemberInputActionForm memberForm = (MemberInputActionForm)form;
		
		MemberInputViewHelper viewhelp =new MemberInputViewHelper();
		
		if(uc.getAttribute(TmsConstants.VIEWHELP)!=null)
		{
			viewhelp =(MemberInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		}
		viewhelp.setFloors("");
		//给ViewHelper赋值
		viewhelp = setViewFromForm(viewhelp,memberForm);		
		
		if(!StringUtil.isNullOrBlank(memberForm.getImgUpLoad1()))
		{
			//
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.ONE);
			uc.removeAttribute(TOCLEAR);
			uc.setAttribute(TOCLEAR, TmsConstants.ONE);
			actionForward = LOADIMG;
		}
		if(!StringUtil.isNullOrBlank(memberForm.getImgUpLoad2()))
		{
			// 
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.TWO);
			uc.removeAttribute(TOCLEAR);
			uc.setAttribute(TOCLEAR, TmsConstants.ONE);
			actionForward = LOADIMG;
		}
		if(!StringUtil.isNullOrBlank(memberForm.getImgUpLoad3()))
		{
			//
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.THREE);
			uc.removeAttribute(TOCLEAR);
			uc.setAttribute(TOCLEAR, TmsConstants.ONE);
			actionForward = LOADIMG;
		}
		if(!StringUtil.isNullOrBlank(memberForm.getImgUpLoad5()))
		{
			//
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.FIVE);
			uc.removeAttribute(TOCLEAR);
			uc.setAttribute(TOCLEAR, TmsConstants.ONE);
			actionForward = LOADIMG;
		}
		if(!StringUtil.isNullOrBlank(memberForm.getToSave()))
		{
			//确认页面
			uc.removeAttribute(TOCLEAR);
			uc.setAttribute(TOCLEAR, TmsConstants.ONE);
			actionForward = TOSAVE;
		}
		//进到页面之前需要将页面信息set到uc中用于保存
		
		
		uc.setAttribute(TmsConstants.VIEWHELP,viewhelp);//将viewhelp放到uc中
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(actionForward);
	}
	
	/**
	 * 
	 * 将form表单内容set到viewhelp中
	 * 
	 * @return
	 */
	private MemberInputViewHelper setViewFromForm(MemberInputViewHelper view,MemberInputActionForm form)
	{
		//商户名称
		view.setRealName(form.getRealName());
		//商户短名
		view.setShortName(form.getShortName());
		//商户名称拼音
		view.setRealNamePy(form.getRealNamePy());
		//联系人
		view.setContactOwner(form.getContactOwner());
		//联系电话
		view.setContactPhone(form.getContactPhone());
		//法人姓名
		view.setLegal(form.getLegal());
		//法人身份证号
		view.setCardNo(form.getCardNo());
		//法人性别
		view.setRegisteredCapital(form.getRegisteredCapital());
		//法人出生日期
		view.setSettlePhone(form.getSettlePhone());
		//法人电话
		view.setPhone(form.getPhone());
		//法人地址
		view.setOrgNo(form.getOrgNo());
		//商户门头照
		view.setMemberZipCode(form.getMemberZipCode());
		//法人身份证图片
		view.setCardPhoto(form.getCardPhoto());
		//组织机构代码证图片
		view.setOrgPhoto(form.getOrgPhoto());
		//营业执照图片
		view.setBusinessPhoto(form.getBusinessPhoto());
		//备注
		view.setCarPortInstr(form.getCarPortInstr());
		//用户名
		view.setUserId(form.getUserId());
		//密码
		view.setPassword(form.getPassword());
		
		return view;
	}
}
